﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tea;

namespace AliYun_RenLian_Obj
{
    internal class Sample_DuanXin
    {
        /// <term><b>Description:</b></term>
        /// <description>
        /// <para>使用凭据初始化账号Client</para>
        /// </description>
        /// 
        /// <returns>
        /// Client
        /// </returns>
        /// 
        /// <term><b>Exception:</b></term>
        /// Exception
        public static AlibabaCloud.SDK.Dysmsapi20170525.Client CreateClient()
        {
            // 工程代码建议使用更安全的无AK方式，凭据配置方式请参见：https://help.aliyun.com/document_detail/378671.html。
            string AccessKeyId = System.Configuration.ConfigurationManager.ConnectionStrings["RenLian_AccessKeyId"].ConnectionString.ToString();
            string AccessKeySecret = System.Configuration.ConfigurationManager.ConnectionStrings["RenLian_AccessKeySecret"].ConnectionString.ToString();

            AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
            {
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
                AccessKeyId = AccessKeyId,
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                AccessKeySecret = AccessKeySecret,
            };
            // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
            config.Endpoint = "dysmsapi.aliyuncs.com";
            return new AlibabaCloud.SDK.Dysmsapi20170525.Client(config);
        }

        public Res_msg Main2(string code,string PhoneNumbers,string TemplateCode)
        {
            Res_msg res_Msg = new Res_msg();
            res_Msg.code = 1;
            duanxin duanxin = new duanxin();
            duanxin.code = code;
            var code_json = JsonConvert.SerializeObject(duanxin);
            AlibabaCloud.SDK.Dysmsapi20170525.Client client = CreateClient();
            AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsRequest sendSmsRequest = new AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsRequest
            {
                TemplateCode = TemplateCode,
                SignName = "驾管易软件",
                PhoneNumbers = PhoneNumbers,
                //TemplateParam = "{\"name\":\"张三\",\"number\":\"1390000****\"}",
                TemplateParam = code_json,
                SmsUpExtendCode = "90999",
                OutId = "abcdefgh",
            };
            AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
            try
            {
                // 复制代码运行请自行打印 API 的返回值
                var resp = client.SendSmsWithOptions(sendSmsRequest, runtime);

                res_Msg.Message = resp.Body.Message;
                return res_Msg;

            }
            catch (TeaException error)
            {
                // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
                // 错误 message
                Console.WriteLine(error.Message);
                // 诊断地址
                Console.WriteLine(error.Data["Recommend"]);
                AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
                res_Msg.code = 0;
                res_Msg.Message = error.Message;
                return res_Msg;
            }
            catch (Exception _error)
            {
                TeaException error = new TeaException(new Dictionary<string, object>
                {
                    { "message", _error.Message }
                });
                // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
                // 错误 message
                Console.WriteLine(error.Message);
                // 诊断地址
                Console.WriteLine(error.Data["Recommend"]);
                AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
                res_Msg.code = 0;
                res_Msg.Message = error.Message;
                return res_Msg;
            }
        }


    }
    public class duanxin
    {
        public string code { get; set; }
    }
}
